package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentFeedbackDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentFeedbackService;

@Tag(name = "管理后台 - 反馈")
@RestController
@RequestMapping("/cmt/content-feedback")
@Validated
public class ContentFeedbackController {

    @Resource
    private ContentFeedbackService feedbackService;

    @PostMapping("/create")
    @Operation(summary = "创建反馈")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:create')")
    public CommonResult<Long> createFeedback(@Valid @RequestBody ContentFeedbackSaveReqVO createReqVO) {
        return success(feedbackService.createFeedback(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新反馈")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:update')")
    public CommonResult<Boolean> updateFeedback(@Valid @RequestBody ContentFeedbackSaveReqVO updateReqVO) {
        feedbackService.updateFeedback(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除反馈")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:delete')")
    public CommonResult<Boolean> deleteFeedback(@RequestParam("id") Long id) {
        feedbackService.deleteFeedback(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除反馈")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:delete')")
    public CommonResult<Boolean> deleteFeedbackList(@RequestParam("ids") List<Long> ids) {
        feedbackService.deleteFeedbackListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:query')")
    public CommonResult<ContentFeedbackRespVO> getFeedback(@RequestParam("id") Long id) {
        ContentFeedbackDO feedback = feedbackService.getFeedback(id);
        return success(BeanUtils.toBean(feedback, ContentFeedbackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得反馈分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:query')")
    public CommonResult<PageResult<ContentFeedbackRespVO>> getFeedbackPage(@Valid ContentFeedbackPageReqVO pageReqVO) {
        PageResult<ContentFeedbackDO> pageResult = feedbackService.getFeedbackPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentFeedbackRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出反馈 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-feedback:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFeedbackExcel(@Valid ContentFeedbackPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentFeedbackDO> list = feedbackService.getFeedbackPage(pageReqVO).getList();
        ExcelUtils.write(response, "反馈.xls", "数据", ContentFeedbackRespVO.class,
                        BeanUtils.toBean(list, ContentFeedbackRespVO.class));
    }

}
